﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using User.XCloudSDK;
using Vlc.DotNet.Wpf;

namespace Ozone.Views.Widget
{
    /// <summary>
    /// UCVideoMonitor.xaml 的交互逻辑
    /// </summary>
    public partial class UCVideoMonitor : UserControl
    {
        public XCloudSDK.PXSDK_MessageCallBack _mainCallback;//回调函数
        public int _hUser = 0;//用户句柄
        public int _hRealPlay = 0;//预览播放句柄
        public object VlcContext { get; private set; }

        VlcVideoSourceProvider sourceProvider;

        public UCVideoMonitor()
        {
            InitializeComponent();

            InitSDK();
        }

        //SDK初始化
        public void InitSDK()
        {
            //Config.ini里填写初始化相关的参数：开发者鉴权信息、配置文件目录等
            string sInit = "{\"LogLevel\":8,\"TempPath\":\"\",\"ConfigPath\":\"\",\"PlatUUID\":\"\",\"PlatAppKey\":\"\",\"PlatAppSecret\":\"\",\"PlatMovedCard\":0,\"ServerIP\":\"\",\"ServerPort\":34567,\"InitType\":0}";
            if (File.Exists("./Config.ini"))
            {
                //如果是合法的Json，就读取配置文件，否则使用默认参数
                string sFileContext = File.ReadAllText("./Config.ini");
                if (sFileContext.StartsWith("{") && sFileContext.EndsWith("}"))
                {
                    sInit = sFileContext;
                }
                else
                {
                    File.WriteAllText("./Config.ini", sInit);
                }
            }
            else
            {
                File.WriteAllText("./Config.ini", sInit);
            }

            Directory.CreateDirectory("./Download");
            Directory.CreateDirectory("./Record");

            XCloudSDK.XCloudSDK_Init(sInit);
            _mainCallback = new XCloudSDK.PXSDK_MessageCallBack(XCloudSDKCallBack);
            _hUser = XCloudSDK.XCloudSDK_RegisterCallback(_mainCallback, IntPtr.Zero);

            // 60005:指定渲染方式，默认使用DirectX
            // 1:DirectX  2:OpenGL  3:GDI
            // XCloudSDK.XSDK_SetSDKIntAttr(60005, 2);

        }

        // 回调函数
        unsafe int XCloudSDKCallBack(System.Int32 hObject, int nMsgId, int nParam1, int nParam2, int nParam3, string szString, IntPtr pObject, Int64 lParam, int nSeq, IntPtr pUserData, IntPtr pMsg)
        {
            Console.WriteLine("MsgId:" + nMsgId + " P1:" + nParam1 + " P2:" + nParam2 + " P3:" + nParam3 + "Seq:" + nSeq);
            Console.WriteLine("Str:" + szString.ToString());

            //如果需要处理回调来的数据，在这里操作

            //实时预览结果回调
            if (nMsgId == (int)ESXSDK_CMD.ESXSDK_MEDIA_START_REAL_PLAY)
            {
                if (nParam1 >= 0)
                {
                    //预览打开成功
                }
                else
                {
                    MessageBox.Show("Error : " + nParam1);
                    _hRealPlay = 0;
                }
            }

            //抓图结果回调
            if (nMsgId == (int)EUIMSG.EUIMSG_PLAY_SAVE_IMAGE_FILE)
            {
                if (nParam1 >= 0)
                {
                    MessageBox.Show("Success");
                }
                else
                {
                    MessageBox.Show("Error : " + nParam1);
                }
            }

            if (nMsgId == (int)EUIMSG.EUIMSG_RECORD_STOP)
            {
                if (nParam1 >= 0)
                {
                    MessageBox.Show("Success : " + szString.ToString());
                }
                else
                {
                    MessageBox.Show("Error : " + nParam1);
                }
            }
            return 1;
        }

        private void playVideo(string devIP)
        {
            //添加设备到设备列表中
            string sDevId = devIP+":34567";
            string sUser = "admin";
            string sPwd = "";

            if (sDevId.Length <= 0 || sUser.Length <= 0)
            {
                MessageBox.Show("Please enter DenId and UserName.");
                return;
            }

            XCloudSDK.XCloudSDK_Device_SetLocalUserNameAndPwd(sDevId, sUser, sPwd);



            _hRealPlay = XCloudSDK.XCloudSDK_Device_MediaRealPlay(_hUser, sDevId, 0, 0, RealPlayWnd.Handle, 0, "");

        }

        public void Start(string devIP)
        {
            

            playVideo(devIP);
        }
    }
}
